﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace Drive.LiveExcelHost.Models
{

    /// <summary>
    /// [{"columns":[{"type":"text","align":"center","width":"212","source":[],"options":[]},{"type":"text","align":"center","width":50,"source":[],"options":[]},{"type":"text","align":"center","width":50,"source":[],"options":[]},{"type":"text","align":"center","width":50,"source":[],"options":[]},{"type":"text","align":"center","width":50,"source":[],"options":[]}],"minDimensions":[5,16],"worksheetName":"Sheet 1","data":[{"row":0,"data":[null,null,null,null,null]},{"row":1,"data":["2",null,null,null,null]},{"row":2,"data":[null,null,null,null,null]},{"row":3,"data":[null,null,null,null,null]},{"row":4,"data":[null,null,null,null,null]},{"row":5,"data":["3336",null,null,null,null]},{"row":6,"data":["3337",null,null,null,null]},{"row":7,"data":["3338",null,null,null,null]},{"row":8,"data":["3339",null,null,null,null]},{"row":9,"data":["3340",null,null,null,null]},{"row":10,"data":["3341",null,null,null,null]},{"row":11,"data":[null,null,null,null,null]},{"row":12,"data":["33333",null,null,null,null]},{"row":13,"data":[null,null,null,null,null]},{"row":14,"data":[null,null,null,null,null]},{"row":15,"data":[null,null,null,null,null]}]}]
    /// </summary>

    public class Spreadsheet : List<Worksheet>
    {
        [Newtonsoft.Json.JsonIgnore]
        public bool ReadOnly { get; set; }

        [Newtonsoft.Json.JsonIgnore]
        public bool DoNotCache { get; set; }

        public static Spreadsheet Error(string title, string message)
        {
            Spreadsheet ss = new Spreadsheet();
            ss.ReadOnly = true;
            ss.DoNotCache = true;
            var sheet = new Worksheet();
            sheet.columns.Add(new Column
            {
                title = title,
                type = "text",
                align = "center",
                width = 1024
            });
            sheet.data.Add(new Datum
            {
                data = new List<string> { message },
                row = 0
            });
            sheet.minDimensions = new int[] { 1, 5 };
            ss.Add(sheet);
            return ss;
        }
        public static Spreadsheet FromDataTable(DataTable dt, bool readOnly, bool doNotCache)
        {
            Spreadsheet ss = new Spreadsheet();
            ss.ReadOnly = readOnly;
            ss.DoNotCache = doNotCache;
            var sheet = new Worksheet();
            foreach (DataColumn item in dt.Columns)
            {
                sheet.columns.Add(new Column
                {
                    title = item.ColumnName,
                    type = "text",
                    align = "center",
                    width = (int)(1024 / dt.Columns.Count)
                });
            }
            foreach (DataRow item in dt.Rows)
            {
                sheet.data.Add(new Datum
                {
                    data = item.ItemArray.Select(x => x?.ToString()).ToList(),
                    row = dt.Rows.IndexOf(item)
                });
            }
            sheet.worksheetName = dt.TableName;
            sheet.minDimensions = new int[] { dt.Columns.Count, dt.Rows.Count };
            ss.Add(sheet);
            return ss;
        }
    }
    public class Worksheet
    {
        public Worksheet()
        {
            this.columns = new List<Column>();
            this.minDimensions = new int[0];
            this.data = new List<Datum>();
        }
        public List<Column> columns { get; set; }
        public int[] minDimensions { get; set; }
        public string worksheetName { get; set; }
        public List<Datum> data { get; set; }
    }

    public class Column
    {
        public string title { get; set; }

        public string type { get; set; }
        public string align { get; set; }
        public object width { get; set; }
        public object[] source { get; set; }
        public object[] options { get; set; }
    }

    public class Datum
    {
        public Datum() { }
        public Datum(int columns)
        {
            this.data = System.Linq.Enumerable.Repeat((string)null, columns).ToList();
        }
        public int row { get; set; }
        public List<string> data { get; set; }
        public string id { get; set; }
    }
}
